package com.avoid.easymqtt.remoting.netty;

import com.avoid.easymqtt.common.SystemClock;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttMessage;

import java.util.Objects;

/**
 * 描述
 *
 * @author avoid@fiture.com
 * @date 2022/5/9
 * @motto Life is so short,do something to make yourself happy,such as coding
 */
public class RequestTask implements Runnable {

    private final Runnable runnable;
    private final long createTimestamp = SystemClock.getNow();
    private final Channel channel;
    private final MqttMessage request;
    private boolean stopRun = false;

    public RequestTask(final Runnable runnable, final Channel channel, final MqttMessage request) {
        this.runnable = runnable;
        this.channel = channel;
        this.request = request;
    }

    @Override
    public int hashCode() {
        int result = runnable != null ? runnable.hashCode() : 0;
        result = 31 * result + (int)(getCreateTimestamp() ^ (getCreateTimestamp() >>> 32));
        result = 31 * result + (channel != null ? channel.hashCode() : 0);
        result = 31 * result + (request != null ? request.hashCode() : 0);
        result = 31 * result + (isStopRun() ? 1 : 0);
        return result;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof RequestTask)) {
            return false;
        }

        final RequestTask that = (RequestTask)o;

        if (getCreateTimestamp() != that.getCreateTimestamp()) {
            return false;
        }
        if (isStopRun() != that.isStopRun()) {
            return false;
        }
        if (!Objects.equals(channel, that.channel)) {
            return false;
        }
        return request != null ? request == that.request : that.request == null;

    }

    public long getCreateTimestamp() {
        return createTimestamp;
    }

    public boolean isStopRun() {
        return stopRun;
    }

    public void setStopRun(final boolean stopRun) {
        this.stopRun = stopRun;
    }

    @Override
    public void run() {
        if (!this.stopRun) {
            this.runnable.run();
        }
    }

}
